<?php
$this->breadcrumbs=array(
	'Templates'=>array('index'),
	'Manage',
);

/** @var CClientScript */
$cs = Yii::app()->clientScript;
$cs->registerCoreScript('jquery.ui');
$cs->registerCssFile($cs->getCoreScriptUrl().'/jui/css/base/jquery-ui.css');
$this->menu=array(
	array('label'=>'Create Template', 'url'=>array('create')),
    array('label'=>'Show selected items', 'url'=>array('status', 'value'=>1), 'linkOptions'=>array('class'=>'change-status')),
    array('label'=>'Hidden selected items', 'url'=>array('status', 'value'=>0), 'linkOptions'=>array('class'=>'change-status')),
);

$script = "
jQuery('#template-grid .status-column a').live('click',function() {
    jQuery.fn.yiiGridView.update('template-grid', {
        url:jQuery(this).attr('href'),
        success:function() {
            jQuery.fn.yiiGridView.update('template-grid');
        }
    });
    return false;
});
jQuery('.crud-menu .change-status').live('click',function() {
    var data = [];
    jQuery.each(jQuery('input.select-on-check'), function(){
        if (jQuery(this).attr('checked'))
            data.push(jQuery(this).val());
    });
    if (data.length <= 0)
    {
        alert('No item selected.');
        return false;
    }
    
    jQuery.fn.yiiGridView.update('template-grid', {
        url:jQuery(this).attr('href'),
        data : {'ids' : data},
        success:function() {
            jQuery.fn.yiiGridView.update('template-grid');
        }
    });
    return false;
});
";
Yii::app()->clientScript->registerScript(__CLASS__.'#ChangeStatus', $script, CClientScript::POS_READY);
?>

<h1 id="title">Manage Templates</h1>

<?php $grid = $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'template-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
    'selectableRows'=>2,
    'selectionChanged'=>"updateSelectors",
	'columns'=>array(
        array(
            'class'=>'CCheckBoxColumn',
            'value'=>'$data->id',
            'htmlOptions'=>array('width'=>'3%'),
        ),
		array(
            'name'=>'name',
            'type'=>'raw',
            'value'=>'CHtml::link($data->name, array("update", "id"=>$data->id))',
        ),
		'group',
		array(
            'name'=>'status',
            'type'=>'raw',
            'value'=>'CHtml::link($data->statusText, array("status", "ids[]"=>$data->id, "value"=>$data->status ? 0 : 1), array("class"=>($data->status ? "active" : "")))',
            'filter'=>false,
            'htmlOptions'=>array(
                'class'=>'status-column'
            )
        ),
		/*
		'creation_datetime',
		'last_update',
		*/
		array(
			'class'=>'CButtonColumn',
			'template'=>'{update} {delete}',
//            'updateButtonUrl'=>'Yii::app()->controller->createUrl("revisions", array("template_id"=>$data->id))'
		),
	),
)); 

if ($grid->dataProvider->ItemCount) {
    $this->menu[] = array('label' => 'Delete selected items', 'url'=>$this->createUrl('delete'), 'linkOptions' => array('onclick'=>'return multipleDelete("template-grid",this.href)'));
}
Yii::app()->clientScript->registerScriptFile(Yii::app()->core->AssetUrl.'/scripts/gridview.js', CClientScript::POS_BEGIN);
?>